package com.chengqj.service;

import com.chengqj.mongodb.domain.OrderDO;
import com.chengqj.mongodb.repository.OrderRepository;
import com.chengqj.mysql.domain.UserDO;
import com.chengqj.mysql.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.List;

/**
 * Copyright © 2020年 tx-demo. All rights reserved.
 *
 * @author chengqj
 * 用户服务类
 * @date 2020/11/13 17:35
 */
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private OrderRepository orderRepository;

    public List<UserDO> loadList(){
        return userRepository.findAll();
    }
    @Transactional
    public void createNewUserAndInitOrder() {
        UserDO user = new UserDO();
        user.setUsername("JACK");
        user.setPassword("123456");
        userRepository.save(user);

        OrderDO order = new OrderDO();
        order.setUserId(user.getId());
        order.setGmtCreate(LocalDateTime.now());
        orderRepository.save(order);

        throw new RuntimeException("意外引发异常");
    }
}
